package com.CST.controller.ys;

import com.CST.common.api.ResultObject;
import com.CST.dto.ys.DataDTO;
import com.CST.dto.ys.UpdateDTO;
import com.CST.dto.ys.YsAppealDTO;
import com.CST.entity.ys.YsAppealRecord;

import com.CST.service.ys.YsAccountDetailService;
import com.CST.service.ys.YsAppealRecordService;
import com.CST.service.ys.YsAssetDetailService;
import com.CST.service.ys.YsIndexDetailService;
import com.CST.vo.ys.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ellen
 * @since 2023-04-27
 */
@RestController
@RequestMapping("/ysAppealRecord")
@Api("申诉记录")
public class YsAppealRecordController {
    private final YsAppealRecordService service;
    private final YsIndexDetailService index;
    private final YsAssetDetailService asset;
    private final YsAccountDetailService account;

    public YsAppealRecordController(YsAppealRecordService service, YsIndexDetailService index, YsAssetDetailService asset, YsAccountDetailService account) {
        this.service = service;
        this.index = index;
        this.asset = asset;
        this.account = account;
    }

    @ApiOperation("根据当前用户查询申诉记录")
    @PostMapping("/listByUser")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "source",value = "区分申诉记录来源"),
            @ApiImplicitParam(name = "subjectName",value = "预算科目名称"),
            @ApiImplicitParam(name = "status",value = "状态")
    })
    public ResultObject<List<YsAppealRecordVO>> pageByUser(@RequestBody YsAppealDTO dto, HttpServletRequest request){
        return ResultObject.success(service.pageByUser(dto,request));
    }

    @ApiOperation("撤回申诉")
    @GetMapping("/delete/{id}")
    public ResultObject<Integer> undoRecord(@PathVariable Long id){
        return ResultObject.success(service.undoRecord(id));
    }

    @ApiOperation("修改申诉")
    @PostMapping("/update")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "主键"),
            @ApiImplicitParam(name = "file",value = "文件标志"),
            @ApiImplicitParam(name = "memo",value = "备注")
    })
    public ResultObject<Boolean> undoRecord(@RequestBody UpdateDTO dto) throws IOException {
        return ResultObject.success(service.updateRecord(dto));
    }

    @ApiOperation("修改回显信息")
    @PostMapping("/get_info")
    public ResultObject<Object> getInfoById(@RequestBody DataDTO dto){
        YsAppealRecord record = service.getById(dto.getId());
        if(!Objects.isNull(record)){
            switch (record.getSource()){
                case 1:
                    AccountDetailVO accountDetailVO = account.getAccountByNum(record.getNum(),record.getYard(),5);
                    return ResultObject.success(accountDetailVO);
                case 2:
                    AccountDetailVO expenseVO = account.getAccountByNum(record.getNum(),record.getYard(),6);
                    return ResultObject.success(expenseVO);
                case 3:
                    AssetDetailVO assetVO = asset.getAssetByNum(record.getNum(),record.getYard());
                    return ResultObject.success(assetVO);
                case 4:
                    YsIndexDetailsVO indexVO= index.getIndexById(record.getNum());
                    return ResultObject.success(indexVO);
            }
        }else{
            throw new RuntimeException("记录不存在");
        }
        return ResultObject.failed("异常");
    }

    @ApiOperation("查看申诉记录详情")
    @PostMapping("/get_obj")
    public ResultObject<Object> getRecordDetailByAppeal(@RequestBody DataDTO dto){
        YsAppealRecord record = service.getById(dto.getId());
        if(!Objects.isNull(record)){
            switch (record.getSource()){
                case 1:
                    YsRecordWithAccountVO accountVO =  service.getRecordAccountDetail(record,5);
                    return  ResultObject.success(accountVO);
                case 2:
                    YsRecordWithExpenseVO expenseVO = service.getRecordExpenseDetail(record,6);
                    return ResultObject.success(expenseVO);
                case 3:
                    YsRecordWithAssetVO assetVO = service.getRecordAssetDetail(record);
                    return ResultObject.success(assetVO);
                case 4:
                    YsRecordWithIndexVO indexVO = service.getRecordIndexDetail(record);
                    return ResultObject.success(indexVO);
            }
        } else {
            throw new RuntimeException("记录不存在");
        }
        return ResultObject.failed("异常");
    }




}
